var app = getApp();
var baseUrl = app.globalData.host;
Component({
  data: {
    formData: {
    },
    rules: [{
          name: 'suggLoanTerm',
          rules:{required : true , message:'请输入贷款期限'},
      }, {
          name: 'loanPeriodLendStableIncome',
          rules: {required: true, message: '请输入贷款期间内借款人及其配偶稳定的工资收入'},
      }, {
          name: 'loanPeriodFamilyLifeExpn',
          rules:{required:true, message: '请输入贷款期间内家庭生活支出'},
      }, {
          name: 'loanTermCustRepayLoanPrin',
          rules: {required: true, message: '请输入贷款期间内客户应偿还的其他贷款本金'},
      }, {
          name: 'remark',
          rules: {required: true, message: '请输入备注说明'},
      }
    ]
  },
  methods: {
    formInputChange(e) {
      const {field} = e.currentTarget.dataset;
      this.setData({
        [`formData.${field}`]: e.detail.value,
      })
    },
    bindOtherAreaBlur: function(e){
      this.setData({
          [`formData.remark`]:e.detail.value
      }) 
  },
    submitForm: function() {
      this.selectComponent('#form').validate((valid, errors) => {
        console.log('valid', valid, errors)
        if (!valid) {
            const firstError = Object.keys(errors)
            if (firstError.length) {
                this.setData({
                    error: errors[firstError[0]].message
                })
            }
        } else {
          var data = this.data.formData;
          console.info(data);
          this.setData({
              disabled:true
          });
          wx.request({
            url: baseUrl+"/report/addRptFarmPftlossInfoList",
            method: 'POST',
            dataType: 'json',
            data: data,
            header: {
              'content-type': 'application/json'
            },
            success: function(res) { 
              wx.showToast({
                title: "保存成功",
                icon: 'none',
                duration: 2000,
                success:function(){
                  setTimeout(function () {
                    //要延时执行的代码
                    wx.navigateBack({ //跳转到前一个页面
                      delta:1,//前一个页面
                    });
                  }, 2000) //延迟时间
                }
              });
              },
              fail: function(res) {
                  wx.showToast({
                  title: '服务器网络错误,请稍后重试',
                  icon: 'loading',
                  duration: 1500
                  })
              },
              complete: function(res) {
              },
          })    
        }
      })
     },

    onLoad: function(option) {
      var that = this;
      that.setData({
        custId:option.custId,
          [`formData.custId`]: option.custId
      });
      var data = that.data.formData;
      wx.request({
        url: baseUrl+"/report/selectRptFarmPftlossInfoList",
        method: 'POST',
        data: data,
        header: {
            'content-type': 'application/json'
        },
        dataType: 'json',
        success: function(res) { 
          console.info(res.data.code);
          if(res.data.code == 2)
          {
            wx.showToast({
              title: res.data.data,
              icon: 'none',
              duration: 2000,
              success:function(){
                setTimeout(function () {
                  //要延时执行的代码
                  wx.navigateBack({ //跳转到前一个页面
                    delta:1,//前一个页面
                  });
                }, 2000) //延迟时间
              }
            });
          }
          else
          {
            that.setData({
              formData: res.data.data,
            })
          }
         },
        fail: function(res) {
            wx.showToast({
            title: '服务器网络错误,请稍后重试',
            icon: 'loading',
            duration: 1500
            })
        },
        complete: function(res) {
        },
      })
    
    }
  }
});